local mieri = fk.CreateSkill{
    name = "ym2__mieri",
    dynamic_desc = function (self, player, lang)
        if Fk:currentRoom():isGameMode("1v2_mode") or Fk:currentRoom():isGameMode("rmt__1v2_mode") or Fk:currentRoom():isGameMode("fuyao_1v2_mode") then
            return "你参与伤害结算后，可获得一个仅有一个触发条件的“控弦”作为首个或最后一个技能。"
        else
            return  "你使用黑色/红色牌结算后，若你使用此牌造成了伤害，则你可获得一个仅有一个触发条件的“控弦”作为首个/最后一个技能。"
        end
    end,
}
Fk:loadTranslationTable{
    ["ym2__mieri"] = "乜日",
    [":ym2__mieri"] = "你使用黑色/红色牌结算后，若你使用此牌造成了伤害，则你可获得一个仅有一个触发条件的“控弦”作为首个/最后一个技能。",
    ["#ym2__mieri1"] = "首",
    ["#ym2__mieri2"] = "尾",
    ["Back"] = "返回",
    ["#ym2__mieri-prompt"] = "是否发动 乜日，获得仅有一个时机的“控弦”",
    ["#ym2__mieri-prompt2"] = "乜日：请选择要获得“控弦”的位置",

    ["$ym2__mieri1"] = "吾欲攀缴依阊阖，挽弓搭矢震羲和。",
    ["$ym2__mieri2"] = "既怀百步穿柳之艺，当效夏羿穿日之绩。",
}
mieri:addEffect(fk.CardUseFinished,{
    can_trigger = function (self, event, target, player, data)
        if player.room:isGameMode("1v2_mode") or player.room:isGameMode("rmt__1v2_mode") or player.room:isGameMode("fuyao_1v2_mode") then return end
        if player:hasSkill(mieri.name) and player == target then
            return data.card and data.card.color ~= Card.NoColor and data.damageDealt
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local choice = room:askToChoice(player,{
            choices = {"#ym2__kongxian1","#ym2__kongxian2","#ym2__kongxian3","Cancel"},
            skill_name = mieri.name,
            prompt = "#ym2__mieri-prompt",
            detailed = false
        })
        if choice ~= "Cancel" then
            event:setCostData(self,choice)
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self)
        local skills = {}
        local skill = "ym2__kongxian"
        for i = 1, 30, 1 do
            if choice == "#ym2__kongxian1" then
                skill = "ym2__kongxian_A"..i
            elseif choice == "#ym2__kongxian2" then
                skill = "ym2__kongxian_B"..i
            else
                skill = "ym2__kongxian_C"..i
            end
            if not player:hasSkill(skill) then
                room:handleAddLoseSkills(player,skill,nil,false,true)
                break
            end
        end
        if data.card.color == Card.Black then
            for _, skill in ipairs(player.player_skills) do
                local name = skill.name
                local str = Fk:translate(":"..name,"zh_CN")
                if str and str ~= "" and not string.find(name,"#") and skill:isPlayerSkill(player) and not skill:isEquipmentSkill(player) then
                    table.insertIfNeed(skills,name)
                end
            end
            if #skills > 0 then
                room:handleAddLoseSkills(player,"-"..table.concat(skills,"|-"),nil,false,true)
            end
        end
        room:handleAddLoseSkills(player,skill,nil,false,true)
        if #skills > 0 then
            room:handleAddLoseSkills(player,table.concat(skills,"|"),nil,false,true)
        end
        room:sendLog{
            type = "#AcquireSkill",
            from = player.id,
            arg = skill
        }
        room.logic:trigger(fk.EventAcquireSkill, player, {skill = Fk.skills[skill],who = player})
    end,
})
mieri:addEffect(fk.DamageFinished,{
    can_trigger = function (self, event, target, player, data)
        if player.room:isGameMode("1v2_mode") or player.room:isGameMode("rmt__1v2_mode") or player.room:isGameMode("fuyao_1v2_mode") then
            return player:hasSkill(mieri.name) and (data.from == player or data.to == player)
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        while true do
            local choice = room:askToChoice(player,{
                choices = {"#ym2__kongxian1","#ym2__kongxian2","#ym2__kongxian3","Cancel"},
                skill_name = mieri.name,
                prompt = "#ym2__mieri-prompt",
                detailed = false
            })
            if choice ~= "Cancel" then
                local top = room:askToChoice(player,{
                    choices = {"#ym2__mieri1","#ym2__mieri2","Back"},
                    skill_name = mieri.name,
                    prompt = "#ym2__mieri-prompt2",
                    detailed = false
                })
                if top ~= "Back" then
                    event:setCostData(self,{choice = choice,top = top})
                    return true
                end
            else
                break
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self).choice
        local top = event:getCostData(self).top
        local skills = {}
        local skill = "ym2__kongxian"
        for i = 1, 30, 1 do
            if choice == "#ym2__kongxian1" then
                skill = "ym2__kongxian_A"..i
            elseif choice == "#ym2__kongxian2" then
                skill = "ym2__kongxian_B"..i
            else
                skill = "ym2__kongxian_C"..i
            end
            if not player:hasSkill(skill) then
                room:handleAddLoseSkills(player,skill,nil,false,true)
                break
            end
        end
        if top == "#ym2__mieri1" then
            for _, s in ipairs(player.player_skills) do
                local name = s.name
                local str = Fk:translate(":"..name,"zh_CN")
                if str and str ~= "" and not string.find(name,"#") and s:isPlayerSkill(player) and not s:isEquipmentSkill(player) then
                    table.insertIfNeed(skills,name)
                end
            end
            if #skills > 0 then
                room:handleAddLoseSkills(player,"-"..table.concat(skills,"|-"),nil,false,true)
            end
        end
        room:handleAddLoseSkills(player,skill,nil,false,true)
        if #skills > 0 then
            room:handleAddLoseSkills(player,table.concat(skills,"|"),nil,false,true)
        end
        room:sendLog{
            type = "#AcquireSkill",
            from = player.id,
            arg = skill
        }
        room.logic:trigger(fk.EventAcquireSkill, player, {skill = Fk.skills[skill],who = player})
    end,
})
return mieri